package cn.shujuhai.qtadmin.platform.config;

import org.jetbrains.annotations.NotNull;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 跨域
 *
 * @author dch798
 * @date 2021/08/23 14:15
 **/
@SpringBootConfiguration
public class CorsConfig implements WebMvcConfigurer {
    /**
     * Configure "global" cross-origin request processing. The configured CORS mappings apply to annotated controllers,
     * functional endpoints, and static resources.
     * <p>
     * Annotated controllers can further declare more fine-grained config via {@link CrossOrigin @CrossOrigin}. In such
     * cases "global" CORS configuration declared here is {@link CorsConfiguration#combine(CorsConfiguration) combined}
     * with local CORS configuration defined on a controller method.
     *
     * @param registry
     *            参数
     * @see CorsRegistry
     * @see CorsConfiguration#combine(CorsConfiguration)
     * @since 4.2
     */
    @Override
    public void addCorsMappings(@NotNull CorsRegistry registry) {
        registry
            // 允许跨域访问的路径
            .addMapping("/**")
            // 允许跨域访问的源
            .allowedOriginPatterns("*")
            // 允许请求方法
            .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
            // 预检间隔时间
            .maxAge(168000)
            // 允许头部设置
            .allowedHeaders("*")
            // 是否发送cookie
            .allowCredentials(true);
    }
}
